﻿using DJ.Dialog;
using DJ.ZEF.ThirdParty;
using NSJL.Biz.Background.Client;
using NSJL.DAL.DataModel.Entities;
using NSJL.DomainModel.Background.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DJ.Pages
{
    /// <summary>
    /// DevicePage.xaml 的交互逻辑
    /// </summary>
    public partial class DevicePage : Page
    {
        CommBiz biz = new CommBiz();
        public DevicePage()
        {
            InitializeComponent();
        }

        private void Button_Select(object sender, RoutedEventArgs e)
        {
            List<DevicesInfo> devicesInfos = biz.GetDevicesInfo(cbDevice.Text == "设备名称" ? null: cbDevice.Text);
            DataGrid1.ItemsSource = devicesInfos;
        }

        private void Button_All(object sender, RoutedEventArgs e)
        {
            List<DevicesInfo> devicesInfos = biz.GetDevicesInfo();
            DataGrid1.ItemsSource = devicesInfos;
        }

        private void Button_Edit(object sender, RoutedEventArgs e)
        {
            DevicesInfo info = (DevicesInfo)DataGrid1.SelectedItem;
            if (info == null)
            {
                MessageDialog.ShowDialog("未选中用户行");
                return;
            }
            DeviceEdit dev = new DeviceEdit();
            dev.name.Text = info.DeviceName;
            dev.name.IsReadOnly = true;
            dev.com.Text = info.COM;
            dev.baud.Text = info.Baud;
            dev.type.Text = info.Type;
            if (dev.ShowDialog() == true)
            {
                var name = dev.name.Text;
                var com = dev.com.Text;
                var baud = dev.baud.Text;
                var type = dev.type.Text;
                if (name.Contains("锁板"))
                {
                    LockManager.GetInstance().Close();
                    LockManager.GetInstance().Start(com, baud.ToInt32());
                }
                var ccresult = biz.EditDevicesInfo(name, com,baud,type,info.Id,info.Count);
                MessageDialog.ShowDialog(ccresult);
            }
            Button_All(null, null);
        }

        private void Button_Delete(object sender, RoutedEventArgs e)
        {
            DevicesInfo info = (DevicesInfo)DataGrid1.SelectedItem;
            if (info == null)
            {
                MessageDialog.ShowDialog("未选中用户行");
                return;
            }

            DeviceDel dev = new DeviceDel();
            dev.name.Content = info.DeviceName;
            dev.com.Content = info.COM;
            dev.baud.Content = info.Baud;
            dev.type.Content = info.Type;
            if (dev.ShowDialog() == true)
            {
                biz.DeleteDevices(info.Id);
                Button_All(null, null);
            }
        }

        private void Button_Add(object sender, RoutedEventArgs e)
        {
            DeviceEdit dev = new DeviceEdit();
            if (dev.ShowDialog() == true)
            {
                var name = dev.name.Text;
                var com = dev.com.Text;
                var baud = dev.baud.Text;
                var type = dev.type.Text;
                var ccresult = biz.EditDevicesInfo(name, com,baud,type);
                MessageDialog.ShowDialog(ccresult);
                Button_All(null, null);
            }
        }

        private void Page_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            List<DevicesInfo> devicesInfos = biz.GetDevicesInfo();
            DataGrid1.ItemsSource = devicesInfos;
            cbDevice.Items.Clear();
            cbDevice.SelectedIndex = 0;
            cbDevice.Items.Add("设备名称");
            foreach (DevicesInfo deviceInfo in devicesInfos)
            {
                cbDevice.Items.Add(deviceInfo.DeviceName);
            }
        }
    }
}
